//
//  HZGoodsDetailToolPanel.m
//  app
//
//  Created by 刘华舟 on 15/6/4.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZGoodsDetailToolPanel.h"
#import "HZGoodsDetailVCData.h"

static const NSInteger kNumCounterHeight = 72;
static const NSInteger kMainViewHeight = kSizeMallViewMargin+kNumCounterHeight;


@interface HZGoodsDetailToolPanel()

@property (strong, nonatomic) UILabel *counterLabel;

@property (strong, nonatomic) UIButton *cartBtn;

@property (strong, nonatomic) UILabel *cornerLabel;

@property (strong, nonatomic) UIButton *joinCartBtn;


@property (strong, nonatomic) NSString *productType;
@property (strong, nonatomic) NSString *startTime;
@property (strong, nonatomic) NSString *endTime;
@property (assign, nonatomic) NSTimeInterval timeOffset;
@property (assign, nonatomic) NSInteger productNum;

@property (strong, nonatomic) NSString *kuchunNum;

@end

@implementation HZGoodsDetailToolPanel



- (instancetype)initWithFrame:(CGRect)frame{
    frame = CGRectOf(frame.origin, [UBO_SCREEN fw:UB_WIDTH uh:kMainViewHeight]);
    if (self = [super initWithFrame:frame]) {
        
        UIButton *cartBtn = [[UIButton alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP],
                                                                     [self.ubo fw:(UB_3_7*UB_HALF) fh:UB_HEIGHT])];

        [cartBtn setTitleColor:HZ_WHITE_COLOR forState:UIControlStateNormal];
        [cartBtn setImage:[UIImage imageNamed:kImageGoodsCartRed] forState:UIControlStateNormal];
        cartBtn.titleLabel.font = JKAppGlobleUFontContentText;
        [cartBtn addTarget:self action:@selector(gotoCartOnClick) forControlEvents:UIControlEventTouchUpInside];
        cartBtn.backgroundColor = HZ_MALL_M_LIGHT_GRAY_BACKGROUND_COLOR;
        [self addSubview:cartBtn];
        
        
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectOf([self.ubo fx:(UB_3_7*UB_HALF) fy:UB_TOP],
                                                                  [self.ubo fw:(UB_4_7*UB_HALF) fh:UB_HEIGHT])];
        leftView.backgroundColor = HZ_MALL_MAIN_THEME_BLACK;
        
        UIImageView *backCounterView = [[UIImageView alloc] initWithFrame:CGRectOf([leftView.ubo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_TOP uy:kSizeMallViewMargin/2],
                                                                                   [leftView.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin fh:UB_HEIGHT uh:-kSizeMallViewMargin])];
        UIImage *counterImage = [[UIImage imageNamed:kImageGoodsDetailNumCounter] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 1, 15, 18) resizingMode:UIImageResizingModeStretch];
        
        [backCounterView setImage:counterImage];
        
        [leftView addSubview:backCounterView];
        
        self.counterLabel = [[UILabel alloc] initWithFrame:CGRectOf([backCounterView.ubo fx:UB_LEFT fy:UB_TOP],
                                                                    [backCounterView.ubo fw:UB_WIDTH uw:-kNumCounterHeight/2 fh:UB_HEIGHT])];
        self.counterLabel.textAlignment = NSTextAlignmentCenter;
        self.counterLabel.textColor = HZ_MALL_TEXT_L_LIGHTGRAY_COLOR;
        self.counterLabel.text = @"1";
        self.counterLabel.font = JKAppGlobleFontSmallTagText;
        [backCounterView addSubview:self.counterLabel];
        
        
        UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRectOf([backCounterView.ufo fx:UB_RIGHT ux:-kNumCounterHeight/2 fy:UB_TOP uy:-kSizeMallViewMargin/2], [leftView.ubo uw:kNumCounterHeight/2+kSizeMallViewMargin*2 fh:UB_HALF])];
        addBtn.titleLabel.font = JKAppGlobleFontContentText;
        [addBtn addTarget:self action:@selector(addBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
        [leftView addSubview:addBtn];
        
        UIButton *reduceBtn = [[UIButton alloc] initWithFrame:CGRectOf([addBtn.ufo fx:UB_LEFT fy:UB_BOTTOM], [leftView.ubo fw:UB_WIDTH fh:UB_HEIGHT])];
        
        [reduceBtn addTarget:self action:@selector(reduceBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
        [leftView addSubview:reduceBtn];
        
        
        
        
        [self addSubview:leftView];
        
        
        UIButton *centerView = [[UIButton alloc] initWithFrame:CGRectOf([leftView.ufo fx:UB_RIGHT fy:UB_TOP],
                                                                        [self.ubo fw:UB_1_4 fh:UB_HEIGHT])];
        [centerView setTitle:@"加入购物车" forState:UIControlStateNormal];
        [centerView setTitleColor:HZ_WHITE_COLOR forState:UIControlStateNormal];
        centerView.titleLabel.font = kHZUFont_Content_M;
        [centerView addTarget:self action:@selector(insertCartOnClick) forControlEvents:UIControlEventTouchUpInside];
        centerView.backgroundColor = HZ_MALL_MAIN_THEME_ORANGE;
        [self addSubview:centerView];
        
        UIButton *rightView = [[UIButton alloc] initWithFrame:CGRectOf([centerView.ufo fx:UB_RIGHT fy:UB_TOP],
                                                                       [self.ubo fw:UB_1_4 fh:UB_HEIGHT])];
        rightView.backgroundColor = HZ_MALL_MAIN_THEME_RED;
        [rightView setTitle:@"立刻购买" forState:UIControlStateNormal];
        [rightView setTitleColor:HZ_WHITE_COLOR forState:UIControlStateNormal];
        rightView.titleLabel.font = kHZUFont_Content_M;
        [rightView addTarget:self action:@selector(buyNowOnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:rightView];
    }
    return self;
}

- (void)gotoCartOnClick{
    if (self.targetCallerGotoCart) {
        [self.targetCallerGotoCart call];
    }
}

- (void)insertCartOnClick{
    if (self.targetCallerInsertCart) {
        [self.targetCallerInsertCart call:self.counterLabel.text];
    }
}

- (void)buyNowOnClick{
    if (self.targetCallerBuyNow) {
        [self.targetCallerBuyNow call:self.counterLabel.text];
    }
}


- (void)addBtnOnClick{
    NSInteger counter = [self.counterLabel.text integerValue];
    counter ++;
    self.counterLabel.text = [NSString stringWithFormat:@"%d", counter];
    self.counterLabel.font = JKAppGlobleUFontSmallTagText;
}

- (void)reduceBtnOnClick{
    NSInteger counter = [self.counterLabel.text integerValue];
    counter --;
    if (counter<=0) {
        counter = 1;
    }
    self.counterLabel.text = [NSString stringWithFormat:@"%d", counter];
    self.counterLabel.font = JKAppGlobleUFontSmallTagText;
}

@end
